package com.store.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.easeui.model.DBHelper_hy;
import com.hyphenate.easeui.model.recordBean_hy;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.fragment.TheOrderBuyFragment;
import com.store.app.fragment.TheOrderFreeFragment;
import com.store.app.fragment.TheOrderHPTFragment;
import com.store.app.fragment.TheOrderSellFragment;
import com.store.app.imlife.activity.ChatActivity;
import com.store.app.utils.SettingData;

/**
 * Created by Administrator on 2016/11/18.
 * 单个订单页面
 */
public class TheSingleOrderActivity extends FragmentActivity {
    private String showFragmentIndex = "0";
    private ImageView public_ll_return;
    private TextView tvTitle;
    private FrameLayout content;
    public Fragment orderFragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private TextView tv_Right1,tv_Right2;
    private String rightURL;
    //数据库存储
    private DBHelper_hy db=null;
    private recordBean_hy rb=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_the_single_order);
        ExitApplication.getInstance().addActivity(this);
        showFragmentIndex = getIntent().getStringExtra("showFragmentIndex");
        initView();
        Log.v("zyl", "showFragmentIndex TheSingleOrderActivity:" + showFragmentIndex);
        if(db==null){
            db=new DBHelper_hy(this);
            rb=new recordBean_hy();
        }
    }

    private void initView() {
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tv_Right1 = (TextView) findViewById(R.id.tv_right1);
        tv_Right1.setVisibility(View.VISIBLE);
        tv_Right1.setBackgroundResource(R.drawable.pic_guanfangkefu);
        tv_Right1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //宅急送
                if (showFragmentIndex.equals("0")) {
                    String chatid = "diandongzhushouserviceagent";
                    String title  ="官方客服";
                    Intent intent = new Intent(TheSingleOrderActivity.this, ChatActivity.class);
                    intent.putExtra("userId", chatid);
                    intent.putExtra("groupName", title);
                    startActivity(intent);
                    try {
                        rb.saveMember(db, "", chatid, title
                                , "","https://oss-img.meitianhui.com/communityDocs/kefu.jpg", "0");
                    }catch (Exception e){
                    }
                }
                //我要批
                else if (showFragmentIndex.equals("1")||showFragmentIndex.equals("3")) {
                    String chatid = "wypcustomerserviceagent";
                    String title  ="官方客服";
                    Intent intent = new Intent(TheSingleOrderActivity.this, ChatActivity.class);
                    intent.putExtra("userId", chatid);
                    intent.putExtra("groupName", title);
                    startActivity(intent);
                    try {
                        rb.saveMember(db, "", chatid, title
                                , "","https://oss-img.meitianhui.com/communityDocs/kefu.jpg", "0");
                    }catch (Exception e){
                    }
                }
                //领了么==>淘淘领
                else if (showFragmentIndex.equals("2")) {
                    String chatid = "llmcustomerserviceagent";
                    String title  ="官方客服";
                    Intent intent = new Intent(TheSingleOrderActivity.this, ChatActivity.class);
                    intent.putExtra("userId", chatid);
                    intent.putExtra("groupName", title);
                    startActivity(intent);
                    try {
                        rb.saveMember(db, "", chatid, title
                                , "","https://oss-img.meitianhui.com/communityDocs/kefu.jpg", "0");
                    }catch (Exception e){
                    }
                }
//                //预售
//                else if (showFragmentIndex.equals("3")) {
//                    tv_Right1.setVisibility(View.GONE);
//                }
            }
        });

        tv_Right2 = (TextView) findViewById(R.id.tv_right2);
        tv_Right2.setBackgroundResource(R.drawable.super_return_help);
        tv_Right2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(TheSingleOrderActivity.this, WebViewTGActivity.class);
                intent.putExtra("title", "帮助");
                intent.putExtra("url", SettingData.SUPER_HELP);
                startActivity(intent);
            }
        });
        if (showFragmentIndex.equals("0")) {
            tv_Right2.setVisibility(View.GONE);
            tvTitle.setText("宅急送");
        } else if (showFragmentIndex.equals("1")) {
            tv_Right2.setVisibility(View.GONE);
            tvTitle.setText("我要批");
        } else if (showFragmentIndex.equals("2")) {
            tv_Right2.setVisibility(View.GONE);
            tvTitle.setText("淘淘领");//领了么==>淘淘领
        }else if (showFragmentIndex.equals("3")) {
            tv_Right2.setVisibility(View.GONE);
            tvTitle.setText("预售");
        }
        content = (FrameLayout) findViewById(R.id.content);
        public_ll_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initFragmentManager();
    }

    private void initFragmentManager() {

        fragmentManager = getSupportFragmentManager();

        transaction = fragmentManager.beginTransaction();


        if (showFragmentIndex.equals("0")) {
            orderFragment = new TheOrderSellFragment();
            transaction.replace(R.id.content, orderFragment);
        } else if (showFragmentIndex.equals("1")) {

            orderFragment = new TheOrderBuyFragment();
            transaction.replace(R.id.content, orderFragment);

        } else if (showFragmentIndex.equals("2")) {
            orderFragment = new TheOrderFreeFragment();
            transaction.replace(R.id.content, orderFragment);
        }
        else if (showFragmentIndex.equals("3")) {
            orderFragment = new TheOrderHPTFragment();
            transaction.replace(R.id.content, orderFragment);
        }
//        transaction.addToBackStack(null);
        transaction.commitAllowingStateLoss();
    }
}
